<template>
  <!-- 引入下拉刷新组件并绑定相关事件 -->
  <Packagepull-downpull-uprefresh 
    :onRefresh="handleRefresh" 
    containerHeight="100vh"
    ref="refreshLoader"
  >
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,index) in switerimglist.data" :key="item.id||index">
        <image v-if="item.bannerSrc" class="swiperbox" :src="item.bannerSrc"/>
      </swiper-item>
    </swiper>
	
	<!-- 体系布局 -->
	<view class="tixibuju">
		<view class="tixibujuleft">
			<p>体系课</p>
			<p>全方位培养岗位胜任力</p>
			<view style="display: flex;">
				<ul>
					<li>系统培养</li>
					<li>快速就业</li>
				</ul>
				<img style="width: 30rpx;height: 30rpx;" src="/static/hongxing.png" />
			</view>
		</view>
	</view>
	
    
    <!-- 新上好课区域 -->
    <view class="new-courses-section">
      <view class="section-header">
        <text class="section-title">新上好课</text>
        <view class="more-btn">
          <text>更多</text>
          <image class="arrow-icon" src="/static/chevron-right.png" mode="widthFix"/>
        </view>
      </view>
      
      <!-- 课程列表 - 使用v-for循环渲染 -->
      <view class="courses-list">
        <view class="course-item" v-for="(course, index) in newCourses" :key="index">
          <image class="course-img" :src="course.imageUrl" mode="widthFix"/>
          <view class="course-info">
            <text class="course-title">{{ course.title }}</text>
            <text class="course-teacher">{{ course.teacher }} {{ course.level }}</text>
            <view class="course-meta">
              <span class="course-price">{{ course.price }}</span>
              <span class="course-students">{{ course.students }}人学习</span>
            </view>
          </view>
        </view>
      </view>
    </view>
    
  </Packagepull-downpull-uprefresh>
</template>

<script>
// 引入下拉刷新组件
import PackagepullDownpullUprefresh from '../../../component/Packagepull-downpull-uprefresh/Packagepull-downpull-uprefresh.vue';
export default {
  components:{
    PackagepullDownpullUprefresh
  },
  data() {
    return {
      switerimglist:[],
      layoutyuijina:[],
      currentindex:0,
      swiperlist:["/static/lunb.png","/static/lunbo2.png"],
      // 新上好课数据
      newCourses: [
        {
          imageUrl: "/static/53.png",
          title: "Al助手Copilot辅助Go+Flutter打造全栈式在线教育系统",
          teacher: "hahsway",
          level: "初阶",
          price: "￥433",
          students: 52
        },
        {
          imageUrl: "/static/51.png",
          title: "全新 云原生系统精讲与全流程落地实践",
          teacher: "hahsway",
          level: "初阶",
          price: "￥199",
          students: 53
        },
        {
          imageUrl: "/static/56.png",
          title: "NestJS 从拔高到精通，大型复杂业务架构落地实践",
          teacher: "hahsway",
          level: "初阶",
          price: "￥699",
          students: 97
        },
    
      ]
    }
  },
  methods: {
    // 下拉刷新处理函数
    async handleRefresh() {
      console.log('开始下拉刷新...');
      
      try {
        // 1. 重新请求轮播图数据
        const swiperRes = await uni.request({
          url: 'http://47.113.230.113:8088/banner/list'
        });
        this.switerimglist = swiperRes.data;
        
        console.log('刷新完成');
      } catch (err) {
        console.error('刷新失败:', err);
      } finally {
        // 无论成功失败都要调用结束刷新
        this.$refs.refreshLoader.finishRefresh();
      }
    }
  },
  async created() {
    try {
      // 推荐轮播布局
      const swiperRes = await uni.request({
        url: 'http://47.113.230.113:8088/banner/list'
      });
      this.switerimglist = swiperRes.data;
      console.log(this.switerimglist, '接口获取的轮播数据');

    } catch (err) {
      console.log('调用失败', err);
    }
  }
}
</script>

<style>
	/* 体系布局 */
	.tixibuju{
		display: flex;
		justify-content: center;
		margin: 20rpx;
	}
	.tixibujuleft{
		display: flex;
		flex-direction: column;
	}
	
	
	
  /* 轮播图 */
  .swiperbox{
    width: 100%;
    height: 300rpx;
    margin-top: 30rpx;
    border-radius: 16rpx;
  }
  
  /* 新上好课区域 */
  .new-courses-section {
    padding: 0 20rpx;
    margin-top: 30rpx;
  }
  
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
    padding: 0 10rpx;
  }
  
  .section-title {
    font-weight: 800;
    font-size: 32rpx;
    color: #333;
  }
  
  .more-btn {
    display: flex;
    align-items: center;
    color: #666;
    font-size: 26rpx;
  }
  
  .arrow-icon {
    width: 30rpx;
    height: 30rpx;
    margin-left: 8rpx;
  }
  
  /* 课程列表 */
  .courses-list {
    display: flex;
    flex-direction: column;
    gap: 20rpx;
  }
  
  .course-item {
    display: flex;
    background-color: #fff;
    border-radius: 16rpx;
    padding: 20rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
    align-items: center;
  }
  
  .course-img {
    width: 150rpx;
    height: 200rpx;
    border-radius: 10rpx;
    flex-shrink: 0;
  }
  
  .course-info {
    margin-left: 20rpx;
    flex-grow: 1;
    overflow: hidden;
  }
  
  .course-title {
    font-size: 28rpx;
    color: #333;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.4;
    margin-bottom: 15rpx;
  }
  
  .course-teacher {
    font-size: 24rpx;
    color: #666;
    display: block;
    margin-bottom: 15rpx;
  }
  
  .course-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .course-price {
    font-size: 26rpx;
    color: #ff4d4f;
    font-weight: bold;
  }
  
  .course-students {
    font-size: 24rpx;
    color: #999;
  }
</style>